package com.nttuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nttuan.model.entity.Clash;
import com.nttuan.mycalender.R;
import com.nttuan.util.Support;

import java.util.Calendar;
import java.util.List;

/**
 * Created by TuanNT on 11/24/2014.
 */
public class ConflictAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<Clash> list;

    public ConflictAdapter(Context context,List<Clash> list) {
        this.list = list;
        this.context =context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHoller holer;
        if(view == null){
            holer = new ViewHoller();
            view = inflater.inflate(R.layout.item_conflict, null);
            holer.taskName =(TextView)view.findViewById(R.id.textTaskNameConflictItem);
            holer.onTime =(TextView)view.findViewById(R.id.textOnTimeConflictItem);
            view.setTag(holer);
        }else
            holer =(ViewHoller) view.getTag();
        Clash clash = list.get(i);
        holer.taskName.setText(clash.getTask().getTaskName());
        int dateOfWeek = Support.dateOfWeek(clash.getDateConflict());
        int currentDate = Support.dateOfWeek(Calendar.getInstance().getTime());
        String sDateOfWeek;
        if(dateOfWeek == dateOfWeek)
            sDateOfWeek =context.getText(R.string.conflict_today).toString();
        else
             sDateOfWeek = context.getResources().getStringArray(R.array.dayOfWeek)[dateOfWeek];
        holer.onTime.setText(context.getString(R.string.conflict_time, Support.displayDate(clash.getDateConflict()),sDateOfWeek));
        return view;
    }
    class ViewHoller{
        TextView taskName;
        TextView onTime;
    }
}
